﻿using Language.Resource;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Filters;
using VisionCloud.Service.Sys;
using VisionCloud.Utility.Extensions;

namespace VisionCloud.Api.Controllers.Sys
{
    /// <summary>
    /// 发送邮件控制器
    /// </summary>
    [SystemModuleAttribute("发送邮件控制器")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiVersion("1.0")]
    [Route("api/sys/[controller]/[Action]")]
    [ApiExplorerSettings(GroupName = "发送邮件控制器")]
    public class EmailController : ApiBaseController
    {
        /// <summary>
        /// 邮件服务
        /// </summary>
        public CrossEmailService CrossEmailService { get; set; }

        /// <summary>
        /// 多语言资源
        /// </summary>
        public IStringLocalizer<ControllerResource> localizer { get; set; }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="emailDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SendEmail(EmailDto emailDto)
        {
            return await TryCatchAsync(CrossEmailService.SendEmail(emailDto.EmailTitle, emailDto.ToEmail, emailDto.EmailContent));
        }

        /// <summary>
        /// 发送邮件，找回密码
        /// </summary>
        /// <param name="emailDto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public async Task<ApiResult> SendEmail2(EmailDto emailDto)
        {
            return await TryCatchAsync(CrossEmailService.SendEmail2(HttpContext.GetClientUserIp(), emailDto.EmailTitle, emailDto.ToEmail, emailDto.EmailContent));
        }

        /// <summary>
        /// 测试发送邮件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> TestEmail()
        {
            return await TryCatchAsync(CrossEmailService.SendEmail("TestTile", "wuqionglei@countrygarden.com.cn", localizer["YB0034"].Value));
        }
    }
}